package org.korifey.ffalg;

import static org.junit.Assert.*;

import java.security.SecureRandom;
import java.util.Random;

import org.junit.Test;


public class FFBitSetTest {
	
	@Test
	public void testSetGetUnset() {
		int n = 1 << 24;
		FFBitSet s = new FFBitSet(n);
		int[] valuesToTest = new int[]{0,1,2,3,4,5,8,9,256,255,257,n/2,n/2-1,n/2+1,n-2,n-1,12345};
		for (int x: valuesToTest) {
			assertFalse(s.get(x));
			s.set(x);
			assertTrue(s.get(x));
		}
		
		for (int i=0; i<valuesToTest.length; i++ ) {
			s.unset(valuesToTest[i]);
			for (int j=0; j<=i; j++ ) {
				assertFalse(s.get(valuesToTest[j]));
			}
			for (int j=i+1; j<valuesToTest.length; j++ ) {
				assertTrue(s.get(valuesToTest[j]));
			}
		}
				
	}
	
	@Test
	public void testRandomGetSetUnset() {
		int n = 1 << 24;
		FFBitSet set = new FFBitSet(n);
		Random r = new SecureRandom();
		for (int i=0; i<100; i++) {
			int x = r.nextInt(n);			
			assertFalse(set.get(x));
			set.set(x);
			assertTrue(set.get(x));
			set.unset(x);
			assertFalse(set.get(x));
			
			//test second time
			set.unset(x);
			assertFalse(set.get(x));
		}
	}
}
